{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pickle\n",
    "import matplotlib.pyplot as plt\n",
    "from scipy import stats\n",
    "import math"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Probabilities**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "N=2\n",
    "v=10\n",
    "c=1\n",
    "T=10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "period=10\n",
    "probF10=pickle.load(open(\"Alternative_Follower_Prob_Period_%s_%s_%s_%s.pkl\"%(period,N,v,c),\"rb\"))\n",
    "probL10=pickle.load(open(\"Alternative_Leader_Prob_Period_%s_%s_%s_%s.pkl\"%(period,N,v,c),\"rb\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "period=9\n",
    "probF9=pickle.load(open(\"Alternative_Follower_Prob_Period_%s_%s_%s_%s.pkl\"%(period,N,v,c),\"rb\"))\n",
    "probL9=pickle.load(open(\"Alternative_Leader_Prob_Period_%s_%s_%s_%s.pkl\"%(period,N,v,c),\"rb\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "period=8\n",
    "probF8=pickle.load(open(\"Alternative_Follower_Prob_Period_%s_%s_%s_%s.pkl\"%(period,N,v,c),\"rb\"))\n",
    "probL8=pickle.load(open(\"Alternative_Leader_Prob_Period_%s_%s_%s_%s.pkl\"%(period,N,v,c),\"rb\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "period=7\n",
    "probF7=pickle.load(open(\"Alternative_Follower_Prob_Period_%s_%s_%s_%s.pkl\"%(period,N,v,c),\"rb\"))\n",
    "probL7=pickle.load(open(\"Alternative_Leader_Prob_Period_%s_%s_%s_%s.pkl\"%(period,N,v,c),\"rb\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "period=6\n",
    "probF6=pickle.load(open(\"Alternative_Follower_Prob_Period_%s_%s_%s_%s.pkl\"%(period,N,v,c),\"rb\"))\n",
    "probL6=pickle.load(open(\"Alternative_Leader_Prob_Period_%s_%s_%s_%s.pkl\"%(period,N,v,c),\"rb\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "period=5\n",
    "probF5=pickle.load(open(\"Alternative_Follower_Prob_Period_%s_%s_%s_%s.pkl\"%(period,N,v,c),\"rb\"))\n",
    "probL5=pickle.load(open(\"Alternative_Leader_Prob_Period_%s_%s_%s_%s.pkl\"%(period,N,v,c),\"rb\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "period=4\n",
    "probF4=pickle.load(open(\"Alternative_Follower_Prob_Period_%s_%s_%s_%s.pkl\"%(period,N,v,c),\"rb\"))\n",
    "probL4=pickle.load(open(\"Alternative_Leader_Prob_Period_%s_%s_%s_%s.pkl\"%(period,N,v,c),\"rb\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "period=3\n",
    "probF3=pickle.load(open(\"Alternative_Follower_Prob_Period_%s_%s_%s_%s.pkl\"%(period,N,v,c),\"rb\"))\n",
    "probL3=pickle.load(open(\"Alternative_Leader_Prob_Period_%s_%s_%s_%s.pkl\"%(period,N,v,c),\"rb\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "period=2\n",
    "probF2=pickle.load(open(\"Alternative_Follower_Prob_Period_%s_%s_%s_%s.pkl\"%(period,N,v,c),\"rb\"))\n",
    "probL2=pickle.load(open(\"Alternative_Leader_Prob_Period_%s_%s_%s_%s.pkl\"%(period,N,v,c),\"rb\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "period=1\n",
    "probF1=pickle.load(open(\"Alternative_Follower_Prob_Period_%s_%s_%s_%s.pkl\"%(period,N,v,c),\"rb\"))\n",
    "probL1=pickle.load(open(\"Alternative_Leader_Prob_Period_%s_%s_%s_%s.pkl\"%(period,N,v,c),\"rb\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Fprobs=[probF1,probF2,probF3,probF4,probF5,probF6,probF7,probF8,probF9,probF10]\n",
    "Lprobs=[probL1,probL2,probL3,probL4,probL5,probL6,probL7,probL8,probL9,probL10]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "probF1=probF6\n",
    "probL1=probL6"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Simulation**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "p2BelowF=[]\n",
    "p2AboveF=[]\n",
    "\n",
    "p6BelowF=[]\n",
    "p6AboveF=[]\n",
    "\n",
    "p10BelowF=[]\n",
    "p10AboveF=[]\n",
    "\n",
    "p2BelowL=[]\n",
    "p2AboveL=[]\n",
    "\n",
    "p6BelowL=[]\n",
    "p6AboveL=[]\n",
    "\n",
    "p10BelowL=[]\n",
    "p10AboveL=[]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "drawslist=[]\n",
    "scoreslist=[]\n",
    "for iter in range(1000000):\n",
    "    scores=[0 for i in range(N)]\n",
    "    draws=[0 for i in range(N)]\n",
    "    for period in range(1,T+1):\n",
    "        FS=int(math.ceil((1-np.exp(-.125*max(scores)))*1000))\n",
    "        mscore=max(scores)\n",
    "        for player in range(N):\n",
    "            if period==1:\n",
    "                if np.random.choice([0,1],p=[1-probF1[0],probF1[0]])==1:\n",
    "                    scores[player]=max(scores[player],np.random.exponential(8))\n",
    "                    draws[player]=draws[player]+1\n",
    "            if period>1:\n",
    "                if leader[player]==1:\n",
    "                    choice=0\n",
    "                    if np.random.choice([0,1],p=[1-Lprobs[period-1][FS],Lprobs[period-1][FS]])==1:\n",
    "                        scores[player]=max(scores[player],np.random.exponential(8))\n",
    "                        draws[player]=draws[player]+1\n",
    "                        choice=1\n",
    "                        \n",
    "                    if (period==2)&(mscore<=15):\n",
    "                        p2BelowL.append(choice)\n",
    "                    if (period==2)&(mscore>15):\n",
    "                        p2AboveL.append(choice)\n",
    "                    if (period==6)&(mscore<=15):\n",
    "                        p6BelowL.append(choice)\n",
    "                    if (period==6)&(mscore>15):\n",
    "                        p6AboveL.append(choice)\n",
    "                    if (period==10)&(mscore<=15):\n",
    "                        p10BelowL.append(choice)\n",
    "                    if (period==10)&(mscore>15):\n",
    "                        p10AboveL.append(choice)\n",
    "                    \n",
    "                elif leader[player]==0:\n",
    "                    choice=0\n",
    "                    if np.random.choice([0,1],p=[1-Fprobs[period-1][FS],Fprobs[period-1][FS]])==1:\n",
    "                        scores[player]=max(scores[player],np.random.exponential(8))\n",
    "                        draws[player]=draws[player]+1\n",
    "                        choice=1\n",
    "                        \n",
    "                    if (period==2)&(mscore<=15):\n",
    "                        p2BelowF.append(choice)\n",
    "                    if (period==2)&(mscore>15):\n",
    "                        p2AboveF.append(choice)\n",
    "                    if (period==6)&(mscore<=15):\n",
    "                        p6BelowF.append(choice)\n",
    "                    if (period==6)&(mscore>15):\n",
    "                        p6AboveF.append(choice)\n",
    "                    if (period==10)&(mscore<=15):\n",
    "                        p10BelowF.append(choice)\n",
    "                    if (period==10)&(mscore>15):\n",
    "                        p10AboveF.append(choice)\n",
    "                    \n",
    "        leader=[0 for i in range(N)]\n",
    "        leader[np.argmax(scores)]=1\n",
    "#             print(leader)\n",
    "#     print(draws)\n",
    "    drawslist.append(sum(draws))\n",
    "    scoreslist.append(max(scores))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(np.array(drawslist).mean(),\"\\n\")\n",
    "print(np.array(p2BelowL).mean(),np.array(p6BelowL).mean(),np.array(p10BelowL).mean(),\"\\n\")\n",
    "print(np.array(p2AboveL).mean(),np.array(p6AboveL).mean(),np.array(p10AboveL).mean(),\"\\n\")\n",
    "print(np.array(p2BelowF).mean(),np.array(p6BelowF).mean(),np.array(p10BelowF).mean(),\"\\n\")\n",
    "print(np.array(p2AboveF).mean(),np.array(p6AboveF).mean(),np.array(p10AboveF).mean(),\"\\n\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
